<?php
namespace app\action;
use core\BaseAction;
use app\model\Label;
use app\model\ArticleLabel;
use core\APP;
use core\JsonInfo;
use app\action\admin\UserAction;
use core\Request;

class BlogAction extends BaseAction{
	function getIndex($params){
		safeParams($params);
		//获取top10文章列表
		$service = App::bean('\\app\\service\\admin\\ArticleService');
		$table = $service->table()->where('status','=',1);
		$pageSize = 5;
		$data = array();
		
		$data['nav'] = 'home';
		
		//条件
		//根据标签，查询
		$title = $params['key'];
		if(!empty($title)){
			$data['title'] = $title;
			$table->where('title','like','%'.$title.'%');
		}
		//根据标签，查询
		$labelId = $params['label'];
		if(!empty($labelId)){
			//查找label名称
			$label = $service->getFirst('id',$labelId,$service->table(Label::$tableName));
			if($label){
				$data['labelName'] = $label->name;
				$table->where('label_json','like','%"id":'.$labelId.',%');
			}
		}
		//根据作者，查询
		$autor = $params['autor'];
		if(!empty($autor)){
			$data['autor'] = $autor;
			$table->where('autor','=',$autor);
		}
		//总页数
		$totals = $table->count();
		$data['pages'] =(int)(($pageSize+$totals-1)/$pageSize);
		
		//排序
		$sort = $params['sort'];
		$table->orderBy('orders','asc');
		if(empty($sort)||"new"==$sort){
			$data['sort'] = 'new';
			$table->orderBy('create_time','desc');
		}else if("hot"==$sort){	//最热,点击最多的
			$data['sort'] = 'hot';
			$table->orderBy('hit_nums','desc');
		}else if("rec"==$sort){	//推荐，推荐最多的
			$data['sort'] = 'rec';
			$table->orderBy('rec_nums','desc');
		}else{
			$data['sort'] = 'new';
			$table->orderBy('create_time','desc');
		}
		$page = $params['page'];
		if(preg_match("/^\d*$/",$page)){
			$page = (int)$page;
			if($page<1||$page>$data['pages']){
				$page = 1;
			}
		}
		else {
			$page=1;
		}
		
		$data['page'] = $page;
		$skipCount = ($page-1)*$pageSize;
		$list = $table->select('id','title','rec_nums','label_json','hit_nums','autor','out_link','summary','create_time')->skip($skipCount)->take($pageSize)->get();
		
		$data['list'] = $list;
		return $this->view("blog.index",$data);
	}
	//xx用户新发布的5篇文章
	function getUaris($params){
		safeParams($params);
		$autor = $params['autor'];
		if(!empty($autor)){
			$html = '<ul class="am-list am-text-sm">';
			$service = App::bean('\\app\\service\\admin\\ArticleService');
			$table = $service->table();
			$table->where('autor','=',$autor);
			$nid = $params['nid'];
			if(!empty($nid)){
				$table->where('id','<>',$nid);
			}
			$table->orderBy('create_time','desc');
			$list = $table->select('id','title','out_link')
				->skip(0)->take(5)->get();
			foreach($list as $i=>$r){
				$link = empty($r->out_link)?CONTEXT_PATH.'/blog/detail?id='.$r->id:$r->out_link;
				$html .= '<li class="am-g"><a class="am-text-truncate appList" href="'.$link.'">'.$r->title.'</a></li>';
			}
			$html .= '</ul>';
		}
		echo $html;
	}
	
	//xx标签下的5篇文章
	function getLaris($params){
		safeParams($params);
		$label = $params['label'];
		if(!empty($label)){
			$html = '<ul>';
			$service = App::bean('\\app\\service\\admin\\ArticleService');
			$table = $service->table();
			$labelArr = explode(',',$label);
			for($i=0;$i<count($labelArr);$i++){
				$table->orWhere('label_json','like','%"id":'.$labelArr[$i].',%');
			}
			$nid = $params['nid'];
			if(!empty($nid)){
				$table->where('id','<>',$nid);
			}
			$table->orderBy('create_time','desc');
			$list = $table->select('id','title','out_link')
				->skip(0)->take(5)->get();
			foreach($list as $i=>$r){
				$link = empty($r->out_link)?CONTEXT_PATH.'/blog/detail?id='.$r->id:$r->out_link;
				$html .= '<li><a class="appList" href="'.$link.'">'.$r->title.'</a></li>';
			}
			$html .= '</ul>';
		}
		echo $html;
	}
	
	
	
	//所有标签
	function getLabels(){
		$service = App::bean('\\app\\service\\admin\\LabelService');
		$list = $service->table()->select('id','name')->get();
		$html = '';
		foreach($list as $i=>$r){
			$count = $service->table(ArticleLabel::$tableName)
				->where('label_id','=',$r->id)->count();
			if($count>0){
				$html .= '<a href="'.CONTEXT_PATH.'/blog/index?label='.$r->id.'" class="am-btn am-btn-secondary  am-btn-xs appBtn">'.$r->name.'&nbsp;&nbsp;<span class="am-badge am-badge-secondary  am-round">'.$count.'</span></a>&nbsp;';
			}
		}
		echo $html;
	}
	
	//详细页面
	function getDetail($params){
		safeParams($params);
		$id = $params['id'];
		//获取数据
		$service = App::bean('\\app\\service\\admin\\ArticleService');
		$table = $service->table()->where('id','=',$id);
		$item = $table->first();
		if(empty($item)){
			echo 404;exit;
		}
		//未发布的，自己和管理员都可以查看
		if($item->status!=1){
			$user  = App::getCurrentUser();
			if($user==null ||($user->is_admin!=1 && $item->user_id!=$user->id)){
				echo $item->user_id.$user->id;
				echo 404;exit;
			}
		}
		
		if(!empty($item->label_json)){
			$jLabel = json_decode($item->label_json);
			$labelIds = array();
			foreach($jLabel as $k=>$r){
				$labelIds[] = $r->id;
			}
			$item->labelIds = implode(',',$labelIds);
		}
		
		return $this->view("blog.detail",['item'=>$item,'nav'=>'home']);
	}
	
	//更新快播
	function getTotal($params){
		//今日更新
		$sql = "";
		$sql .= "select count(1) as num from article_text where date_format(create_time,'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')";
		//本周
		$sql .= "union all SELECT count(1) as num FROM article_text WHERE YEARWEEK(date_format(create_time,'%Y-%m-%d')) = YEARWEEK(now())";
		//本月
		$sql .= "union all select count(1) as num from article_text where date_format(create_time,'%Y-%m')=date_format(now(),'%Y-%m')";
		
		$list = App::DB()->select($sql);
		$todays = $list[0]->num;
		$weeks = $list[1]->num;
		$months = $list[2]->num;
		
		$html = '<ul style="margin-top: 0.5rem;" class="am-list am-list-static am-list-border">';
		$html .= '<li><i class="am-icon-circle-o am-icon-fw"></i>&nbsp;<span class="am-badge am-badge-warning" style="margin-top:0.3rem;">'.$todays.'</span>今日更新:</li>';
		$html .= '<li><i class="am-icon-square-o am-icon-fw"></i>&nbsp;<span class="am-badge am-badge-warning" style="margin-top:0.3rem;">'.$weeks.'</span>本周更新:</li>';
		$html .= ' <li><i class="am-icon-square am-icon-fw"></i>&nbsp;<span class="am-badge am-badge-warning" style="margin-top:0.3rem;">'.$months.'</span>本月更新:</li>';
		$html .= '</ul>';
		
		echo $html;			
	}
	
	//用户注册
	function postReg($params){
		safeParams($params);
		$info = new JsonInfo();
		$params['is_admin'] = 0;
		if(empty($params['login_name'])){
			$info->msg = "非法数据";
		}else{
			$params['login_name']=safeParams($params['login_name']);
			if(preg_match("/[^a-zA-Z0-9_]/",$params['login_name'])){
				$info->msg = "非法用户名";
			}else{
				if(strlen($params['login_name'])>20){
					$info->msg = "用户名太长";
				}else{
					$service = App::bean('\\app\\service\\admin\\UserService');
					$pwd = $params['login_pwd'];
					if(!empty($params['login_pwd'])){
						$params['login_pwd'] = md5($params['login_pwd']);
					}
					$service->saveUser($params,$info);
					$params['login_pwd']  = $pwd;
				}
			}
		}
		//注册成功，自动登陆
		if($info->success){
			$userAct = new UserAction();
			return $userAct->postLogin($params,new Request());
		}
		return $this->json($info);
	}
	
	//推荐
	function postRec($params){
		safeParams($params);
		$info = new JsonInfo();
		if(App::getCurrentUser()==null){
			$info->msg = "请登陆后再操作！";
			return $this->json($info);
		}
		$type = $params['type'];
		$id = $params['id'];
		if(!empty($id)&&($type=='1'||$type=='0')){
			$service = App::bean('\\app\\service\\admin\\ArticleService');
			$service->recArticle($id,$type,$info);
		}
		return $this->json($info);
	}
	
	function postVinc($params){
		safeParams($params);
		$id = $params['id'];
		if(!empty($id)){
			$service = App::bean('\\app\\service\\admin\\ArticleService');
			$service->table()->where('id','=',$id)->increment('hit_nums');
			echo 1;
		}
	}
	
}